Imports cmplib01
Imports FMPLib01
Imports System.Text

Public Class frmBattlesListing
    Inherits System.Windows.Forms.Form

#Region " Windows Form Designer generated code "

    Public Sub New()
        MyBase.New()

        'This call is required by the Windows Form Designer.
        InitializeComponent()

        'Add any initialization after the InitializeComponent() call

    End Sub

    'Form overrides dispose to clean up the component list.
    Protected Overloads Overrides Sub Dispose(ByVal disposing As Boolean)
        If disposing Then
            If Not (components Is Nothing) Then
                components.Dispose()
            End If
        End If
        MyBase.Dispose(disposing)
    End Sub

    'Required by the Windows Form Designer
    Private components As System.ComponentModel.IContainer

    'NOTE: The following procedure is required by the Windows Form Designer
    'It can be modified using the Windows Form Designer.  
    'Do not modify it using the code editor.
    Friend WithEvents Panel1 As System.Windows.Forms.Panel
    Friend WithEvents lblBattlesListing As System.Windows.Forms.Label
    Friend WithEvents Label2 As System.Windows.Forms.Label
    Friend WithEvents pnlTopBorder As System.Windows.Forms.Panel
    Friend WithEvents picCampaignerIcon As System.Windows.Forms.PictureBox
    Friend WithEvents txtTitleBox As System.Windows.Forms.TextBox
    Friend WithEvents rtbBattlesListing As System.Windows.Forms.RichTextBox
    Friend WithEvents picMap As System.Windows.Forms.PictureBox
    Friend WithEvents webBattles As System.Windows.Forms.WebBrowser
    Friend WithEvents btnOK As System.Windows.Forms.Button
    <System.Diagnostics.DebuggerStepThrough()> Private Sub InitializeComponent()
        Dim resources As System.ComponentModel.ComponentResourceManager = New System.ComponentModel.ComponentResourceManager(GetType(frmBattlesListing))
        Me.Panel1 = New System.Windows.Forms.Panel
        Me.picMap = New System.Windows.Forms.PictureBox
        Me.rtbBattlesListing = New System.Windows.Forms.RichTextBox
        Me.lblBattlesListing = New System.Windows.Forms.Label
        Me.Label2 = New System.Windows.Forms.Label
        Me.pnlTopBorder = New System.Windows.Forms.Panel
        Me.picCampaignerIcon = New System.Windows.Forms.PictureBox
        Me.txtTitleBox = New System.Windows.Forms.TextBox
        Me.btnOK = New System.Windows.Forms.Button
        Me.webBattles = New System.Windows.Forms.WebBrowser
        Me.Panel1.SuspendLayout()
        CType(Me.picMap, System.ComponentModel.ISupportInitialize).BeginInit()
        Me.pnlTopBorder.SuspendLayout()
        CType(Me.picCampaignerIcon, System.ComponentModel.ISupportInitialize).BeginInit()
        Me.SuspendLayout()
        '
        'Panel1
        '
        Me.Panel1.Anchor = CType((((System.Windows.Forms.AnchorStyles.Top Or System.Windows.Forms.AnchorStyles.Bottom) _
                    Or System.Windows.Forms.AnchorStyles.Left) _
                    Or System.Windows.Forms.AnchorStyles.Right), System.Windows.Forms.AnchorStyles)
        Me.Panel1.BackColor = System.Drawing.Color.LightSkyBlue
        Me.Panel1.Controls.Add(Me.webBattles)
        Me.Panel1.Controls.Add(Me.picMap)
        Me.Panel1.Controls.Add(Me.rtbBattlesListing)
        Me.Panel1.Controls.Add(Me.lblBattlesListing)
        Me.Panel1.Controls.Add(Me.Label2)
        Me.Panel1.Location = New System.Drawing.Point(8, 48)
        Me.Panel1.Name = "Panel1"
        Me.Panel1.Size = New System.Drawing.Size(496, 482)
        Me.Panel1.TabIndex = 22
        '
        'picMap
        '
        Me.picMap.Location = New System.Drawing.Point(291, 11)
        Me.picMap.Name = "picMap"
        Me.picMap.Size = New System.Drawing.Size(27, 20)
        Me.picMap.TabIndex = 5
        Me.picMap.TabStop = False
        Me.picMap.Visible = False
        '
        'rtbBattlesListing
        '
        Me.rtbBattlesListing.Anchor = CType((((System.Windows.Forms.AnchorStyles.Top Or System.Windows.Forms.AnchorStyles.Bottom) _
                    Or System.Windows.Forms.AnchorStyles.Left) _
                    Or System.Windows.Forms.AnchorStyles.Right), System.Windows.Forms.AnchorStyles)
        Me.rtbBattlesListing.BorderStyle = System.Windows.Forms.BorderStyle.FixedSingle
        Me.rtbBattlesListing.Location = New System.Drawing.Point(319, 359)
        Me.rtbBattlesListing.Name = "rtbBattlesListing"
        Me.rtbBattlesListing.ScrollBars = System.Windows.Forms.RichTextBoxScrollBars.ForcedVertical
        Me.rtbBattlesListing.Size = New System.Drawing.Size(169, 113)
        Me.rtbBattlesListing.TabIndex = 4
        Me.rtbBattlesListing.Text = ""
        Me.rtbBattlesListing.Visible = False
        '
        'lblBattlesListing
        '
        Me.lblBattlesListing.Font = New System.Drawing.Font("Microsoft Sans Serif", 12.0!, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, CType(0, Byte))
        Me.lblBattlesListing.ForeColor = System.Drawing.Color.Black
        Me.lblBattlesListing.Location = New System.Drawing.Point(16, 6)
        Me.lblBattlesListing.Name = "lblBattlesListing"
        Me.lblBattlesListing.Size = New System.Drawing.Size(160, 26)
        Me.lblBattlesListing.TabIndex = 3
        Me.lblBattlesListing.Text = "Battles Listing"
        '
        'Label2
        '
        Me.Label2.Font = New System.Drawing.Font("Microsoft Sans Serif", 9.75!, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, CType(0, Byte))
        Me.Label2.ForeColor = System.Drawing.Color.Black
        Me.Label2.Location = New System.Drawing.Point(16, 8)
        Me.Label2.Name = "Label2"
        Me.Label2.Size = New System.Drawing.Size(104, 16)
        Me.Label2.TabIndex = 3
        Me.Label2.Text = "Select A Battle"
        '
        'pnlTopBorder
        '
        Me.pnlTopBorder.Anchor = CType(((System.Windows.Forms.AnchorStyles.Top Or System.Windows.Forms.AnchorStyles.Left) _
                    Or System.Windows.Forms.AnchorStyles.Right), System.Windows.Forms.AnchorStyles)
        Me.pnlTopBorder.BackColor = System.Drawing.Color.SteelBlue
        Me.pnlTopBorder.Controls.Add(Me.picCampaignerIcon)
        Me.pnlTopBorder.Controls.Add(Me.txtTitleBox)
        Me.pnlTopBorder.Location = New System.Drawing.Point(0, 0)
        Me.pnlTopBorder.Name = "pnlTopBorder"
        Me.pnlTopBorder.Size = New System.Drawing.Size(520, 40)
        Me.pnlTopBorder.TabIndex = 21
        '
        'picCampaignerIcon
        '
        Me.picCampaignerIcon.Image = CType(resources.GetObject("picCampaignerIcon.Image"), System.Drawing.Image)
        Me.picCampaignerIcon.Location = New System.Drawing.Point(9, 11)
        Me.picCampaignerIcon.Name = "picCampaignerIcon"
        Me.picCampaignerIcon.Size = New System.Drawing.Size(16, 16)
        Me.picCampaignerIcon.TabIndex = 9
        Me.picCampaignerIcon.TabStop = False
        '
        'txtTitleBox
        '
        Me.txtTitleBox.BackColor = System.Drawing.Color.SteelBlue
        Me.txtTitleBox.BorderStyle = System.Windows.Forms.BorderStyle.None
        Me.txtTitleBox.Font = New System.Drawing.Font("Microsoft Sans Serif", 14.25!, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, CType(0, Byte))
        Me.txtTitleBox.ForeColor = System.Drawing.Color.WhiteSmoke
        Me.txtTitleBox.Location = New System.Drawing.Point(32, 8)
        Me.txtTitleBox.Multiline = True
        Me.txtTitleBox.Name = "txtTitleBox"
        Me.txtTitleBox.ReadOnly = True
        Me.txtTitleBox.Size = New System.Drawing.Size(312, 26)
        Me.txtTitleBox.TabIndex = 8
        Me.txtTitleBox.Text = "Campaigner [Assign To Battles]"
        '
        'btnOK
        '
        Me.btnOK.Anchor = CType((System.Windows.Forms.AnchorStyles.Bottom Or System.Windows.Forms.AnchorStyles.Right), System.Windows.Forms.AnchorStyles)
        Me.btnOK.BackColor = System.Drawing.Color.LightSkyBlue
        Me.btnOK.FlatStyle = System.Windows.Forms.FlatStyle.Flat
        Me.btnOK.ForeColor = System.Drawing.Color.Black
        Me.btnOK.Location = New System.Drawing.Point(429, 544)
        Me.btnOK.Name = "btnOK"
        Me.btnOK.Size = New System.Drawing.Size(75, 23)
        Me.btnOK.TabIndex = 23
        Me.btnOK.Text = "OK"
        Me.btnOK.UseVisualStyleBackColor = False
        '
        'webBattles
        '
        Me.webBattles.Anchor = CType((((System.Windows.Forms.AnchorStyles.Top Or System.Windows.Forms.AnchorStyles.Bottom) _
                    Or System.Windows.Forms.AnchorStyles.Left) _
                    Or System.Windows.Forms.AnchorStyles.Right), System.Windows.Forms.AnchorStyles)
        Me.webBattles.Location = New System.Drawing.Point(4, 27)
        Me.webBattles.MinimumSize = New System.Drawing.Size(20, 20)
        Me.webBattles.Name = "webBattles"
        Me.webBattles.Size = New System.Drawing.Size(488, 451)
        Me.webBattles.TabIndex = 6
        '
        'frmBattlesListing
        '
        Me.AutoScaleBaseSize = New System.Drawing.Size(5, 13)
        Me.BackColor = System.Drawing.Color.Black
        Me.ClientSize = New System.Drawing.Size(512, 574)
        Me.Controls.Add(Me.btnOK)
        Me.Controls.Add(Me.Panel1)
        Me.Controls.Add(Me.pnlTopBorder)
        Me.Name = "frmBattlesListing"
        Me.ShowInTaskbar = False
        Me.StartPosition = System.Windows.Forms.FormStartPosition.CenterParent
        Me.Panel1.ResumeLayout(False)
        CType(Me.picMap, System.ComponentModel.ISupportInitialize).EndInit()
        Me.pnlTopBorder.ResumeLayout(False)
        Me.pnlTopBorder.PerformLayout()
        CType(Me.picCampaignerIcon, System.ComponentModel.ISupportInitialize).EndInit()
        Me.ResumeLayout(False)

    End Sub

#End Region

    Private ptrGameData As CampaignerGameData
    Private ptrFMPData As FlexMapData
    Private ptrBMP As Bitmap

    Private executionPath As String

    Shadows Sub ShowDialog(ByRef gameData As CampaignerGameData, ByRef fmp As FlexMapData, ByRef mapBmp As Bitmap)

        ' populate the text box with all of the battles information. This will be expanded to show elements
        ' when they are available
        Dim batt As Battle
        Dim battKey As String
        Dim battleList As New StringBuilder()

        ptrGameData = gameData
        ptrFMPData = fmp
        ptrBMP = mapBmp

        '// this is to save the images
        executionPath = My.Application.Info.DirectoryPath

        For Each battKey In gameData.Battles_g.HT.Keys
            batt = gameData.Battles_g.Item(battKey)
            ' add each battle
            battleList.Append("--------------------------------------------")
            battleList.Append(WriteBattle(batt))
        Next

        ' put the battlelist stuff into the richtextbox
        rtbBattlesListing.Text = battleList.ToString

        rtbBattlesListing.Text = AsHTML()


        webBattles.AllowNavigation = False
        'Dim htmlDoc As HtmlDocument
        'htmlDoc.Write(rtbBattlesListing.Text)

        webBattles.DocumentText = rtbBattlesListing.Text.Replace("<img src = " & ControlChars.Quote, "<img src = " & ControlChars.Quote & executionPath & "/battles/")


        MyBase.ShowDialog()

        '// this overrides old the html... we need to change it back
        'rtbBattlesListing.Text = webBattles.DocumentText()

        rtbBattlesListing.SelectAll()
        rtbBattlesListing.SaveFile(executionPath & "/battles/" & ptrGameData.GameName & ptrGameData.Turn & ".html", RichTextBoxStreamType.PlainText)

    End Sub

    Private Function WriteBattle(ByVal Batt As Battle) As String
        Dim battleString As New StringBuilder()

        Try
            ' first line is the battle name
            battleString.Append(Batt.BattleName & ControlChars.CrLf)

            ' second line is the terrain types
            battleString.Append("Terrain Description: " & ptrFMPData.TerrainNameAtCoord(Batt.BattleLocation) & ControlChars.CrLf)

            Dim facta As String = ptrGameData.Faction_g.Item(CampaignerGameData.CDGKEY_FACTION & Batt.DefendingFactionID).FactionName
            Dim factb As String = ptrGameData.Faction_g.Item(CampaignerGameData.CDGKEY_FACTION & Batt.AttackingFactionID).FactionName

            ' fifth line skipped
            battleString.Append(ControlChars.CrLf)

            ' sixth line is the 'defending' players forces involved-> playername: unitname, pv, orders
            battleString.Append(facta & " Units" & ControlChars.CrLf)
            battleString.Append(listInvolvedUnitsHTML(Batt, Batt.DefendingFactionID, "<br/>") & ControlChars.CrLf)

            ' after those lines are the rules for the 'defending' player
            battleString.Append(facta & " Rules Assigned" & ControlChars.CrLf)
            battleString.Append(ListAssignedRules(Batt, True) & ControlChars.CrLf)

            ' then attacking player forces
            battleString.Append(factb & " Units" & ControlChars.CrLf)
            battleString.Append(listInvolvedUnitsHTML(Batt, Batt.AttackingFactionID, "<br/>") & ControlChars.CrLf)

            ' attacking player rules
            battleString.Append(factb & " Rules Assigned" & ControlChars.CrLf)
            battleString.Append(ListAssignedRules(Batt, False) & ControlChars.CrLf)
        Catch ex As Exception
            HandleException("battlelisting", ex)
        End Try

        Return battleString.ToString

    End Function

    Private Function listInvolvedUnitsHTML(ByVal batt As Battle, ByVal validFactionID As Int32, ByVal htmlTag As String) As String
        ' returns the units involved, from the attacking or defending faction 
        Dim playKey As String
        Dim play As Player
        Dim Karayzee As New StringBuilder()

        Dim reinforcementString As String

        Try
            For Each mem As ChitMember In batt.InvolvedChits
                reinforcementString = ""
                If mem.OwningFaction.FactionID = validFactionID Then

                    If mem.UnitType <> GroundChitClass_cgd.Objective Then

                        For Each playKey In ptrGameData.Players.Keys
                            play = ptrGameData.Players.Item(playKey)

                            If play.PlayerID = mem.OwningPlayer.PlayerID Then
                                If mem.currentBattlePosture = BattlePosture_cgd.AttackingSupport Or mem.currentBattlePosture = BattlePosture_cgd.DefendingSupport Then
                                    reinforcementString = "Reinforcement"
                                End If

                                '// put the image of this group here
                                mem.icon.Save(executionPath & "/battles/img/" & mem.Name.Replace(" ", "_").Replace(".", "_") & ".png", System.Drawing.Imaging.ImageFormat.Png)
                                Karayzee.Append("<img src = " & ControlChars.Quote & "img/" & mem.Name.Replace(" ", "_").Replace(".", "_") & ".png" & ControlChars.Quote & ">" & ControlChars.CrLf) '& GetBattlegroupText(mem) & ControlChars.CrLf & htmlTag)
                                Karayzee.Append(" " & mem.Name & " [" & play.PlayerName & "] " & reinforcementString & ControlChars.CrLf & htmlTag)

                                If mem.UnitType = GroundChitClass_cgd.Battlegroup Then Karayzee.Append(GetBattlegroupText(mem, htmlTag))


                                Exit For
                            End If
                        Next
                    Else
                        '// its the same faction, so this is an installation
                        If mem.Status <> GroundUnitStatus_cgd.Dead Then
                            Karayzee.Append(" " & mem.Name & " - installation" & ControlChars.CrLf & htmlTag)
                        End If
                    End If

                End If
            Next
        Catch ex As Exception
            HandleException("battlelisting.listinvolvedunits", ex)
        End Try

        Return Karayzee.ToString
    End Function

    Private Function ListSubUnits(ByRef mem As ChitMember) As String
        Try

        Catch ex As Exception
            HandleException(c_Module, ex)
        End Try
    End Function

    Private Function ListInvolvedUnitsO(ByVal Batt As Battle, ByVal validFactionID As Int32, Optional ByVal htmlTag As String = "") As String
        ' returns the units involved, from the attacking or defending faction 
        Dim playKey As String
        Dim play As Player
        Dim Karayzee As New StringBuilder()

        Dim reinforcementString As String

        Try
            For Each mem As ChitMember In Batt.InvolvedChits
                reinforcementString = ""
                If mem.OwningFaction.FactionID = validFactionID Then

                    If mem.UnitType <> GroundChitClass_cgd.Objective Then

                        For Each playKey In ptrGameData.Players.Keys
                            play = ptrGameData.Players.Item(playKey)

                            If play.PlayerID = mem.OwningPlayer.PlayerID Then
                                If mem.currentBattlePosture = BattlePosture_cgd.AttackingSupport Or mem.currentBattlePosture = BattlePosture_cgd.DefendingSupport Then
                                    reinforcementString = "Reinforcement"
                                End If
                                Karayzee.Append(" " & mem.Name & " [" & play.PlayerName & "] " & reinforcementString & ControlChars.CrLf & htmlTag)

                                If mem.UnitType = GroundChitClass_cgd.Battlegroup Then Karayzee.Append(GetBattlegroupText(mem, htmlTag))
                                Exit For
                            End If
                        Next
                    Else
                        '// its the same faction, so this is an installation
                        If mem.Status <> GroundUnitStatus_cgd.Dead Then
                            Karayzee.Append(" " & mem.Name & " - installation" & ControlChars.CrLf & htmlTag)
                        End If
                    End If

                End If
            Next
        Catch ex As Exception
            HandleException("battlelisting.listinvolvedunits", ex)
        End Try

        Return Karayzee.ToString

    End Function

    Private Function GetBattlegroupText(ByRef bg As BattlegroupChit, Optional ByVal htmlTag As String = "", Optional ByVal listBG As Boolean = False) As String
        Dim un As ChitMember
        Dim cha As New StringBuilder

        If listBG = True Then cha.Append(" " & bg.Name & ControlChars.CrLf & htmlTag)
        For Each un In bg.members

            If un.UnitType = GroundChitClass_cgd.Battlegroup Then
                un.icon.Save(executionPath & "/battles/img/" & un.Name.Replace(" ", "_").Replace(".", "_") & ".png", System.Drawing.Imaging.ImageFormat.Png)
                cha.Append("<img src = " & ControlChars.Quote & "img/" & un.Name.Replace(" ", "_").Replace(".", "_") & ".png" & ControlChars.Quote & "> " & un.Name & "<br/>" & GetBattlegroupText(un) & ControlChars.CrLf & htmlTag)
                cha.Append("<br/>")
            Else
                Dim unt As ChitUnitTypeMember = un
                cha.Append("  <img src = " & ControlChars.Quote & "img/unit.PNG" & ControlChars.Quote & "> " & un.Name & ControlChars.CrLf & "<br/>")
                '// don't forget indicators and elements
                cha.Append("<h5>")
                cha.Append("<font color="& controlchars.Quote & "navy" & ControlChars.Quote & ">")
                If unt.elements.Count > 0 Then
                    For Each eel As Element In unt.elements
                        cha.Append("(" & eel.Count & ") " & eel.ElementTypeName & ControlChars.CrLf & "<br/>")
                    Next
                End If
                cha.Append("</font>")
                cha.Append("<font color=" & ControlChars.Quote & "maroon" & ControlChars.Quote & ">")
                If unt.Indicators.Count > 0 Then
                    For Each indi As IIndicatorEntry In unt.Indicators
                        cha.Append(indi.displayText & ControlChars.CrLf & "<br/>")
                    Next
                End If
                cha.Append("</font>")
                cha.Append("</h5>")
            End If
        Next

        Return cha.ToString
    End Function

    Private Function ListAssignedRules(ByVal Batt As Battle_2, ByVal DefenderPOV As Boolean) As String
        'Dim ruleKey As String
        'Dim asgnRule As AssignedRule
        Dim Karayzee As New StringBuilder
        'Dim HT As Hashtable
        Dim col As ChitRules_c


        If DefenderPOV = True Then
            '    HT = Batt.DefenderAssigned
            col = Batt.DefenderAssigned
        Else
            '   HT = Batt.AttackerAssignedRules
            col = Batt.AttackerAssigned
        End If

        '        For Each ruleKey In HT.Keys
        '       asgnRule = HT.Item(ruleKey)
        Karayzee.Append("<ul>")

        For Each ru As Rule In col
            Karayzee.Append("<li><b>" & ru.ptrRule.RuleName & ":</b>   ") ') '"[" & asgnRule.Count & "]" & ControlChars.CrLf)
            Karayzee.Append("   '" & ru.ptrRule.RuleText & "'</li>")
        Next
        Karayzee.Append("</ul>")

        'If HT.Count = 0 Then
        If col.Count = 0 Then
            Karayzee.Append("<none>" & ControlChars.CrLf)
        End If

        Return Karayzee.ToString

    End Function

    Private Sub btnOK_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnOK.Click
        Me.Hide()
    End Sub

    Private Function AsHTML() As String
        Dim sb As New StringBuilder
        sb.Append("<html>" & vbCrLf)
        sb.Append(" <body>" & vbCrLf)

        For Each battkey As String In ptrGameData.Battles_g.HT.Keys
            Dim batt As Battle = ptrGameData.Battles_g.Item(battkey)
            sb.Append(WriteHTMLBattle(batt))

            sb.Append("<hr/>" & vbCrLf)
            sb.Append("<br/>" & vbCrLf)
            sb.Append("<br/>" & vbCrLf)
            sb.Append("<br/>" & vbCrLf)
        Next

        sb.Append(" </body>" & vbCrLf)
        sb.Append("</html>" & vbCrLf)

        Return sb.ToString
    End Function


    Private Function WriteHTMLBattle(ByVal batt As Battle) As String

        Dim sb As New StringBuilder

        sb.Append("<h2>" & batt.BattleName & "</h2>" & vbCrLf)
        sb.Append("<h3>Terrain Description: " & ptrFMPData.TerrainNameAtCoord(batt.BattleLocation) & "</h3><br/>" & vbCrLf)
        '// we need the battle picture right here
        Dim battlerectangle As Rectangle = New Rectangle(batt.BattleLocation.X - 64, batt.BattleLocation.Y - 64, 128, 128)
        Dim battleBMP As Bitmap = Copybmp(ptrBMP, battlerectangle)
        Dim g As Graphics = Graphics.FromImage(battleBMP)

        Dim zoomBMP As New Bitmap(256, 256)
        Dim battG As Graphics = Graphics.FromImage(zoomBMP)

        battG.DrawImage(battleBMP, 0, 0, 256, 256)

        '// draw all of the combatants
        Dim chitLoc As Point

        For Each mem As ChitMember In batt.InvolvedChits
            chitLoc = New Point((mem.PreciseLocation.X) - batt.BattleLocation.X, (mem.PreciseLocation.Y) - batt.BattleLocation.Y)
            chitLoc.X *= 2
            chitLoc.Y *= 2
            chitLoc.X += 128 - mem.icon.Size.Width / 2
            chitLoc.Y += 128 - mem.icon.Size.Height / 2 '8 '// the 8 is half the drawing size. :) its seperate for clarity, if you can call it that. :P
            If mem.UnitType = GroundChitClass_cgd.Objective Then
                battG.DrawImage(DirectCast(mem, InstallationChit).ptrInstallation.Icon(0), chitLoc.X, chitLoc.Y, mem.icon.Size.Width, mem.icon.Size.Height)
            Else
                battG.DrawImage(mem.icon, chitLoc.X, chitLoc.Y, mem.icon.Size.Width, mem.icon.Size.Height)
            End If

        Next

        '// save the battleimage in the img folder
        zoomBMP.Save(executionPath & "/battles/img/" & batt.Key & ".png", Drawing.Imaging.ImageFormat.Png)

        sb.Append("<img src = " & ControlChars.Quote & "img/" & batt.Key & ".png" & ControlChars.Quote & "> <br/>" & vbCrLf)
        sb.Append("<br/>")
        sb.Append("<h6> Unit composition will be listed below all battles. </h6>")
        sb.Append("<br/>")
        '// now list the battlegroups, and their icons?
        sb.Append(Me.listInvolvedUnitsHTML(batt, batt.AttackingFactionID, "<br/>"))
        sb.Append(Me.ListAssignedRules(batt, False))

        '// now the other side
        sb.Append(Me.listInvolvedUnitsHTML(batt, batt.DefendingFactionID, "<br/>"))
        sb.Append(Me.ListAssignedRules(batt, True))
        sb.Append("<br/>")


        Return sb.ToString
    End Function


    Public Overloads Shared Function Copybmp(ByVal srcBitmap As Bitmap, ByVal section As Rectangle) As Bitmap

        ' Create the new bitmap and associated graphics object
        Dim bmp As New Bitmap(section.Width, section.Height)
        Dim g As Graphics = Graphics.FromImage(bmp)

        ' Draw the specified section of the source bitmap to the new one
        g.DrawImage(srcBitmap, 0, 0, section, GraphicsUnit.Pixel)

        ' Clean up
        g.Dispose()

        ' Return the bitmap
        Return bmp
    End Function

End Class
